package org.jeecg.modules.FinanceModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.entity.RuturnMoneyEntity;
import org.jeecg.modules.FinanceModule.mapper.ReturnMoneyMapper;
import org.jeecg.modules.FinanceModule.service.FinanceService;
import org.jeecg.modules.FinanceModule.service.ReturnMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;

@Service
public class ReturnMoneyServiceImpl implements ReturnMoneyService {

    @Autowired
    ReturnMoneyMapper mapper;

    @Autowired
    FinanceService financeservice;

    @Override
    public IPage<RuturnMoneyEntity> getReturnMoneylist(Page<RuturnMoneyEntity> page, String orderSn, String companyCode, Integer status, String companyName) {
        IPage<RuturnMoneyEntity> pageList = mapper.getReturnMoneylist(page,orderSn,companyCode,status,companyName);

        return pageList;
    }


    //拒绝一个订单
    @Override
    public int refuseApply(int id, String userId, String money, String orderSn) {

        //通过退款表id获取售后订单id
        String orderAfterId=mapper.getorderAfterIdbyretrurnId(id);
        String storeId=mapper.getstoreIdbyorderSn(orderSn);
        //通过userId,查询权限
        String quanxian=mapper.getquanxianbyuserId(Integer.parseInt(userId) );
        //往业务流水表中插入退款数据
        //往售后订单跟踪记录表中插入一条数据
        mapper.insertAfterOrdertrackRunfund(System.currentTimeMillis(),orderAfterId,"所申请的退款被拒绝",new Date(),12);
        mapper.insertrefundwater(String.valueOf(System.currentTimeMillis()),0,new Date(),orderSn,money,userId,quanxian,storeId,"退款已拒绝",4);
        return mapper.refuseApply(id);
    }

    //同意退款
    @Override
    public int approve(int id, String userId, String money, String remark, int orderType, String cartId, String orderMoney, String orderSn) {

        //获取老的存款
        String oldmoney = financeservice.getoldmoney(Integer.parseInt(userId));
        BigDecimal oldmoney2=new BigDecimal(oldmoney);

        //如果是面料的 执行下面
        if(orderType==0){
            mapper.updategouwuchestatus(Integer.parseInt(cartId));

            //通过退款id关联面料订单表id获取三个付款金额
            HashMap<String, BigDecimal> threeMoneymap=mapper.getthreeMoneymap(id);


            double bili=Double.parseDouble(money)/Double.parseDouble(orderMoney);


            DecimalFormat df   = new DecimalFormat("######0.00");
            bili=Double.parseDouble( df.format(bili));


            BigDecimal bili2=BigDecimal.valueOf(bili);

            //花费的抵扣额度
            BigDecimal deductionpay=threeMoneymap.get("deduction_pay");
           //获取老的抵扣额度
            String olddeductionLimit=mapper.getolddeductionLimit(Integer.parseInt(userId));
            BigDecimal olddeductionLimit2=new BigDecimal(olddeductionLimit);
            //添加后修改抵扣额度
           // mapper.updateuserDeductionLimit(Integer.parseInt(userId),String.valueOf( olddeductionLimit2.add( deductionpay.multiply(bili2) )  ));
            mapper.updateuserDeductionLimit(Integer.parseInt(userId),String.valueOf( olddeductionLimit2.add( deductionpay.multiply(BigDecimal.valueOf(Double.parseDouble(money)/Double.parseDouble(orderMoney))) )  ));
            //花费的月结额度
            System.out.println("bxbx"+ String.valueOf( oldmoney2.add( deductionpay.multiply(BigDecimal.valueOf(Double.parseDouble(money)/Double.parseDouble(orderMoney))) )  ));
            BigDecimal monthpay=threeMoneymap.get("month_pay");
            //获取老的剩余
             String  oldremainingLimit=mapper.getoldremainingLimit(Integer.parseInt(userId));
             BigDecimal oldremainingLimit2=new BigDecimal(oldremainingLimit);
             //添加后修改剩余月结额度
            // mapper.updateuserRemainingLimit(Integer.parseInt(userId),String.valueOf( oldremainingLimit2.add( monthpay.multiply(bili2) )  ));
            mapper.updateuserRemainingLimit(Integer.parseInt(userId),String.valueOf( oldremainingLimit2.add( monthpay.multiply(BigDecimal.valueOf(Double.parseDouble(money)/Double.parseDouble(orderMoney))) )  ));
            //花费余额
            System.out.println("bxbx"+ String.valueOf( oldmoney2.add( monthpay.multiply(BigDecimal.valueOf(Double.parseDouble(money)/Double.parseDouble(orderMoney))) )  ));
            BigDecimal balancepay=threeMoneymap.get("balance_pay");
            //添加后修改余额

         //   mapper.updateAccountBalance(Integer.parseInt(userId), String.valueOf( oldmoney2.add( balancepay.multiply(bili2) )  ));
            mapper.updateAccountBalance(Integer.parseInt(userId), String.valueOf( oldmoney2.add( balancepay.multiply(BigDecimal.valueOf(Double.parseDouble(money)/Double.parseDouble(orderMoney))) )  ));
            System.out.println("bxbx"+ String.valueOf( oldmoney2.add( balancepay.multiply(BigDecimal.valueOf(Double.parseDouble(money)/Double.parseDouble(orderMoney))) )  ));
            //通过退款表id获取售后订单id
            String orderAfterId=mapper.getorderAfterIdbyretrurnId(id);
            //往售后订单跟踪记录表中插入一条数据
            mapper.insertAfterOrdertrackRunfund(System.currentTimeMillis(),orderAfterId,"退款金额："+money,new Date(),11);

            //修改这条售后的实际退款金额,
            mapper.updaterealRefund(Integer.parseInt(orderAfterId) ,money);

            //通过userId,查询权限
            String quanxian=mapper.getquanxianbyuserId(Integer.parseInt(userId) );



            String storeId=mapper.getstoreIdbyorderSn(orderSn);

            //往业务流水表中插入退款数据
            mapper.insertrefundwater(String.valueOf(System.currentTimeMillis()),orderType,new Date(),orderSn,money,userId,quanxian,storeId,"退款信息",3);

            return mapper.approve(id,remark,money,userId);
        }




        //退款回去的钱加上原来的余额
        double newmoney = Double.parseDouble(money) + Double.parseDouble(oldmoney);
        //将增加好的值修改回去
        mapper.updateAccountBalance(Integer.parseInt(userId), String.valueOf(newmoney));
        //将申请表中的状态改为同意

        //通过退款表id获取售后订单id
        String orderAfterId=mapper.getorderAfterIdbyretrurnId(id);
        //往售后订单跟踪记录表中插入一条数据
        mapper.insertAfterOrdertrackRunfund(System.currentTimeMillis(),orderAfterId,"退款金额："+money,new Date(),11);
        return mapper.approve(id,remark,money, userId);
    }


    //通过员工id查询父级公司所有者id
    @Override
    public String getCompanyuserId(String userId) {
        return mapper. getCompanyuserId( Integer.parseInt(userId )) ;
    }
}
